package com.libertyville.adapter.driven.persistence.orgmng;

import com.libertyville.domain.orgmng.Tenant;
import com.libertyville.domain.orgmng.TenantRepository;
import com.libertyville.domain.orgmng.TenantStatus;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Repository
public class TenantRepositoryImpl implements TenantRepository {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public boolean existsByIdAndStatus(Integer tenantId, TenantStatus status) {
        List<Tenant> tenants = entityManager
                .createQuery("from Tenant as t where t.tenantId = :tenantId and t.tenantStatus = :status", Tenant.class)
                .setParameter("tenantId", tenantId)
                .setParameter("status", status)
                .getResultList();
        return tenants.size() == 1;
    }
}
